/*
 * UIActionHandler.fx
 *
 * Created on Apr 14, 2010, 10:07:50 PM
 */

package dawnland03.ui.model.action.handler;

import dawnland03.ui.service.UIService;
import dawnland03.model.action.Action;
import dawnland03.model.action.ActionType;
import dawnland03.model.action.parameter.ActionOutputParam;
import dawnland03.model.entity.Entity;
import dawnland03.ui.model.UIEntity;
import javafx.animation.KeyFrame;
import dawnland03.ui.model.UIHexagon;
import javafx.animation.Timeline;
import dawnland03.model.entity.EntitySnapshot;
import dawnland03.ui.model.entity.UIEntityModel;
import java.util.List;


/**
 * @author user
 */

public class UIActionHandlerImpl extends UIActionHandler{
    override public function handle (action:Action) : Void {
       var owner = action.getOutputParameters().getOutputParameter(ActionOutputParam.OWNER_SNAPSHOT) as EntitySnapshot;
       if(action.getActionType() == ActionType.INIT){
               handleInit(action, owner);
       } else if(action.getActionType() == ActionType.MOVE){
               handleMove(action, owner);
       } else if(action.getActionType() == ActionType.GATHER){
               handleGather(action, owner);
       }

    }

    function handleInit(action:Action, owner:EntitySnapshot) : Void {
       var currentMattrixCellId = owner.getMatrixCellId();
       UIService.getUIService().createUIEntity(owner.getId(), currentMattrixCellId);
    }

    function handleMove(action:Action, owner:EntitySnapshot) : Void {
       var uiEntity: UIEntity = UIService.getUIService().getUIEntity(owner.getId());
       var destinationMatrixCells = action.getOutputParameters().getOutputParameter(ActionOutputParam.DESTINATION) as List;
       var destinationMatrixCellId = destinationMatrixCells.get(0) as Integer; //for a move action, the path was only 1 step (one matrixCell)
       var destinationUIHexagon : UIHexagon = UIService.getUIService().getUIHexagon(destinationMatrixCellId);

       var keyFrames : KeyFrame[];

       def tempUIEntity = uiEntity;
       var moveKeyFrame = KeyFrame{
               time: 1s;
               values: [
                   tempUIEntity.centerX => destinationUIHexagon.getCenterXToParent(),
                   tempUIEntity.centerY => destinationUIHexagon.getCenterYToParent(),
                   ]
            };
       insert moveKeyFrame into keyFrames;

       var lastKeyFrame = KeyFrame {
           time: 1001ms;
           action: function(): Void{updateUIEntityModel(owner)};
       };

       insert lastKeyFrame into keyFrames;

       Timeline {
            keyFrames: keyFrames;
        }.play();
      }

    function handleGather(action:Action, owner:EntitySnapshot) : Void {
       var keyFrames : KeyFrame[];

       var lastKeyFrame = KeyFrame {
           time: 1001ms;
           action: function(): Void{updateUIEntityModel(owner)};
       };

       insert lastKeyFrame into keyFrames;

       Timeline {
            keyFrames: keyFrames;
        }.play();
      }

      function updateUIEntityModel(entitySnapshot : EntitySnapshot){
          var uiEntityModel : UIEntityModel = UIService.getUIService().getUIEntityModel(entitySnapshot.getId());
          uiEntityModel.update(entitySnapshot);
      }
}
